package generator;

import generator.meta.ClassMeta;
import generator.meta.FieldMeta;
import generator.util.JsUIUtils;
import generator.util.PackageScan;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-8-9
 * Time: 上午9:23
 * To change this template use File | Settings | File Templates.
 */
public class WebGenerator extends Generator {
    public void generateWebJs( String packageName, String classPackage) throws Exception {
        Set<Class<?>> classes = PackageScan.getClasses(classPackage);
        for (Class<?> clazz : classes) {
            ClassMeta classMeta = new ClassMeta();
            classMeta.setLowerClassName(clazz.getSimpleName().substring(0,1).toLowerCase()+clazz.getSimpleName().substring(1));
            Field[] fields = clazz.getDeclaredFields();
            classMeta.setFieldSize(fields.length);
            List<FieldMeta> fieldMetas = new ArrayList<FieldMeta>();
            for (Field field : fields) {
                MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
                String fieldPackageName=field.getGenericType().toString().replace("class","").replace(classPackage,packageName);
                FieldMeta fieldMeta = new FieldMeta();
                fieldMeta.setFormUI(JsUIUtils.getFormUI(field,fieldPackageName));
                fieldMeta.setComment(annotation.comments());
                fieldMeta.setColumnUI(JsUIUtils.getColumnUI(field, annotation.comments(), classMeta));
                fieldMeta.setName(field.getName());
                fieldMetas.add(fieldMeta);
            }
            classMeta.setFields(fieldMetas);
            Map root = new HashMap();
            root.put("bean", classMeta);
            super.generateWebJs(root, "init", "init", classMeta.getLowerClassName());
            super.generateWebJs(root, "form", "form",classMeta.getLowerClassName() );
        }
    }

    public void generateWebJsp(String classPackage) throws Exception {
        Set<Class<?>> classes = PackageScan.getClasses(classPackage);
        List<ClassMeta> classMetas =  new ArrayList<ClassMeta>();
        for (Class<?> clazz : classes) {
            ClassMeta classMeta = new ClassMeta();
            classMeta.setLowerClassName(clazz.getSimpleName().substring(0,1).toLowerCase()+clazz.getSimpleName().substring(1));
            Field[] fields = clazz.getDeclaredFields();
            classMeta.setFieldSize(fields.length);
            List<FieldMeta> fieldMetas = new ArrayList<FieldMeta>();
            for (Field field : fields) {
                MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
                FieldMeta fieldMeta = new FieldMeta();
                fieldMeta.setSimpleSearchUI(JsUIUtils.getSimpleSearchUI(field,annotation.comments(),classMeta));
                fieldMeta.setAdvanceSearchUI(JsUIUtils.getAdvanceSearchUI(field, annotation.comments(), classMeta));
                fieldMeta.setInputUI(JsUIUtils.getInputSearchUI(field, annotation.comments(), classMeta));
                fieldMeta.setViewUI(JsUIUtils.getViewSearchUI(field,annotation.comments(),classMeta));
                fieldMeta.setName(field.getName());
                fieldMetas.add(fieldMeta);
            }
            MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);
            classMeta.setFields(fieldMetas);
            classMeta.setMenuUI(JsUIUtils.getMenuUI(classMeta,annotation.comments()));
            Map root = new HashMap();
            root.put("bean", classMeta);
            super.generateWebJsp(root, "index", "index", classMeta.getLowerClassName());
            super.generateWebJsp(root, "input", "input", classMeta.getLowerClassName());
            super.generateWebJsp(root, "view", "view", classMeta.getLowerClassName());
            classMetas.add(classMeta);
        }
        Map index = new HashMap();
        index.put("bean", classMetas);
        super.generateWebIndexJsp(index, "main");
    }
}
